package com.hb.trade.server.center.au.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.center.au.entity.UserVerify;
import com.hb.core.pojo.center.au.mapper.UserVerifyMapper;
import com.hb.core.pojo.parent.MPCommonEntity;
import com.hb.trade.server.center.au.service.UserVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.enums.AuditStatusEnum;
import org.springframework.stereotype.Service;
/**
 * <p>
 * 用户认证表, 用户注册时的认证方式 实现类
 * </p>
 *
 * @author ljs
 * @since 2022-3-23 10:10:21
 */
@Slf4j
@Service
public class UserVerifyServiceImpl extends ServiceImpl<UserVerifyMapper, UserVerify> implements UserVerifyService {

    @Override
    public Integer getVerifyType(Long userId) {
        QueryWrapper<UserVerify> wrapper = UserVerify.wrapper();
        wrapper.eq("user_id",userId).last("limit 1");
        UserVerify userVerify = getOne(wrapper);
        return userVerify == null ? null : userVerify.getVerifyType();
    }

    @Override
    public <T> T getBackInfo(Long userId, Class<T> backInfoClass) {
        if (userId == null) {
            return null;
        }
        QueryWrapper<UserVerify> wrapper = UserVerify.wrapper();
        wrapper.lambda().eq(UserVerify::getUserId, userId).last("limit 1");
        UserVerify verify = getOne(wrapper);
        if (verify == null) {
            return null;
        }
        String backInfo = verify.getBackInfo();
        if (StrUtil.isEmpty(backInfo)) {
            return null;
        }
        return JSONUtil.toBean(backInfo, backInfoClass);
    }

    @Override
    public UserVerify findAuditPassInfoByUserIdAndVerifyType(Long userId, Integer verifyType) {
        if (userId == null || verifyType == null) {
            return null;
        }
        QueryWrapper<UserVerify> wrapper = UserVerify.wrapper();
        wrapper.lambda().eq(UserVerify::getUserId, userId)
                .eq(UserVerify::getVerifyType, verifyType)
                .eq(UserVerify::getAuditStatus, AuditStatusEnum.PASSED.getCode())
                .last("limit 1");
        UserVerify verifyInfo = getOne(wrapper);
        return verifyInfo;
    }
}
